package com.xy.novelmanagement.service.impl;

import com.xy.model.RootNovelType;
import com.xy.novelmanagement.mapper.NovelClassificationMapper;
import com.xy.novelmanagement.service.INovelClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class NovelClassificationServiceImpl implements INovelClassificationService {
		@Autowired
		private NovelClassificationMapper classificationMapper;

		//log.info("NovelClassificationServiceImpl");
		@Override
		public List<RootNovelType> getTypeList() {
				List<RootNovelType> list = null;
				try {
						list = classificationMapper.getTypeList();
						log.info("NovelClassificationServiceImpl getTypeList list = "+list);
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
				}
				return list;
		}

		@Override
		@Transactional
		public Integer addType(RootNovelType novelType) {
				Integer code = -1;
				log.info("NovelClassificationServiceImpl addType novelType = " + novelType);
				try {
						String typeName = classificationMapper.selectTypeByName(novelType.getTypeName());
						typeName = typeName == null ? "" : typeName;
						log.info("NovelClassificationServiceImpl addType selectName = " + typeName);
						if (typeName.equals(novelType.getTypeName())) {
								return 200;
						}
						int row = classificationMapper.insertType(novelType.getTypeName());
						if (row != 1) {
								return -200;
						} else {
								code = 100;
						}
						log.info("NovelClassificationServiceImpl addTyp row = "+row);
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
				}
				return code;
		}

		@Override
		@Transactional
		public int updateType(String typeName, RootNovelType rootNovelType) {
				int result = 0;
				log.info("NovelClassificationServiceImpl updateType typename = "+typeName );
				log.info("NovelClassificationServiceImpl updateType rootNovelType = "+rootNovelType );
				try {
						String name = rootNovelType.getTypeName();
						Integer row = classificationMapper.getTypeByName(typeName);
						log.info("NovelClassificationServiceImpl updateType row = "+row );
						if(row>1||row==0){
								return -1;
						}else {
								result = classificationMapper.updateType(typeName, name)?1:0;
						}
						log.info("NovelClassificationServiceImpl updateType result = "+result );
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
						return 0;
				}
				return result;
		}

		@Override
		@Transactional
		public boolean deleteType(String[] idList) {
				boolean row =false;

				try {

						row  = classificationMapper.deleteByNames(idList);
				} catch (Exception e) {
						log.error("错误消息：{}", e.getMessage(), e);
						return false;
				}
				return row;
		}
}
